package com.hainatong.mvvmbase.utils;

import java.util.Locale;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;


/**
 * 本地SharePreference工具
 * <p>
 * Created By Rikka on 2019/7/12
 */
public class SharePreferenceUtil
{
    private static final String TAG = "SharePreferenceUtil";

    public static String SHARED_PREFERENCE_FILE_NAME = "language_sp";

    public static String TAG_LANGUAGE = "languageSelect";

    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;
    private static SharePreferenceUtil mInstance;
    private Locale systemCurrentLocal = Locale.CHINESE;

    private SharePreferenceUtil()
    {
    }

    @SuppressLint("CommitPrefEdits")
    private static void init(Context context)
    {
        if (sp == null)
        {
            sp = context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        }
        editor = sp.edit();
    }

    public static SharePreferenceUtil getInstance(Context context)
    {
        if (mInstance == null)
        {
            synchronized (SharePreferenceUtil.class)
            {
                if (mInstance == null)
                {
                    init(context);
                    mInstance = new SharePreferenceUtil();
                }
            }
        }
        return mInstance;
    }

    public void saveLanguage(int select)
    {
        editor.putInt(TAG_LANGUAGE, select);
        editor.commit();
    }

    int getSelectLanguage()
    {
        return getInt(TAG_LANGUAGE, 0);
    }

    Locale getSystemCurrentLocal()
    {
        return systemCurrentLocal;
    }

    public void setSystemCurrentLocal(Locale local)
    {
        systemCurrentLocal = local;
    }

    public void remove(String key)
    {
        editor.remove(key).apply();
    }

    private String getString(String key, String defaultValue)
    {
        return sp.getString(key, defaultValue);
    }

    private void saveLong(String key, long values)
    {
        editor.putLong(key, values);
    }

    private long getLong(String key, long defaultValue)
    {
        return sp.getLong(key, defaultValue);
    }

    private int getInt(String key, int defaultValue)
    {
        return sp.getInt(key, defaultValue);
    }

    private void saveString(String key, String value)
    {
        editor.putString(key, value).apply();
    }
}
